<template>
  <el-dialog
    :title="!dataForm.attrId ? '新增' + titleT : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="外键，类型id" prop="catId">
      <el-input size="mini"  v-model="dataForm.catId" placeholder="外键，类型id"></el-input>
    </el-form-item>
    <el-form-item label="属性名称" prop="attrName">
      <el-input size="mini"  v-model="dataForm.attrName" placeholder="属性名称"></el-input>
    </el-form-item>
    <el-form-item label="only:输入框(唯一)  many:后台下拉列表/前台单选框" prop="attrSel">
      <el-input size="mini"  v-model="dataForm.attrSel" placeholder="only:输入框(唯一)  many:后台下拉列表/前台单选框"></el-input>
    </el-form-item>
    <el-form-item label="manual:手工录入  list:从列表选择" prop="attrWrite">
      <el-input size="mini"  v-model="dataForm.attrWrite" placeholder="manual:手工录入  list:从列表选择"></el-input>
    </el-form-item>
    <el-form-item label="可选值列表信息,例如颜色：白色,红色,绿色,多个可选值通过逗号分隔" prop="attrVals">
      <el-input size="mini"  v-model="dataForm.attrVals" placeholder="可选值列表信息,例如颜色：白色,红色,绿色,多个可选值通过逗号分隔"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input size="mini"  v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          attrId: undefined,
          catId: '',
          attrName: '',
          attrSel: '',
          attrWrite: '',
          attrVals: '',
        },
        dataRule: {
          catId: [
            { required: true, message: '外键，类型id不能为空', trigger: 'blur' }
          ],
          attrName: [
            { required: true, message: '属性名称不能为空', trigger: 'blur' }
          ],
          attrSel: [
            { required: true, message: 'only:输入框(唯一)  many:后台下拉列表/前台单选框不能为空', trigger: 'blur' }
          ],
          attrWrite: [
            { required: true, message: 'manual:手工录入  list:从列表选择不能为空', trigger: 'blur' }
          ],
          attrVals: [
            { required: true, message: '可选值列表信息,例如颜色：白色,红色,绿色,多个可选值通过逗号分隔不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    computed: {
    },
    methods: {
      init (attrId) {
        this.dataForm.attrId = attrId 
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.attrId) {
            this.$http({
              url: this.$http.adornUrl(`/goods/goodsattribute/info/${this.dataForm.attrId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.goodsAttribute
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/goods/goodsattribute/${!this.dataForm.attrId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
